package definite.Guide.Book.JFileChooser;

import java.awt.Dimension;
import java.awt.Image;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.File;

import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JLabel;

// A custom accessory for use with JFileChooser

public class LabelAccessory extends JLabel implements PropertyChangeListener {
	// field
	private static final int PREFERRED_WIDTH = 125;
	private static final int PREFERRED_HEIGHT = 100;

	public LabelAccessory(JFileChooser chooser) {
		setVerticalAlignment(JLabel.CENTER);
		setHorizontalAlignment(JLabel.CENTER);
		chooser.addPropertyChangeListener(this);
		setPreferredSize(new Dimension(PREFERRED_WIDTH, PREFERRED_HEIGHT));

	}

	@Override
	public void propertyChange(PropertyChangeEvent evt) {
		String changeName = evt.getPropertyName();
		if (changeName.equals(JFileChooser.SELECTED_FILE_CHANGED_PROPERTY)) {
			// Neu la file thi hien len label , khong la File thi khong hien len
			if (evt.getNewValue() instanceof File) {
				File file = (File) evt.getNewValue();
				ImageIcon icon = new ImageIcon(file.getPath());
				if (icon.getIconWidth() > PREFERRED_WIDTH) {
					icon =
						new ImageIcon(icon.getImage().getScaledInstance(
								PREFERRED_WIDTH, -1, Image.SCALE_DEFAULT));
					if (icon.getIconHeight() > PREFERRED_HEIGHT)
						icon =
							new ImageIcon(icon.getImage().getScaledInstance(-1,
									PREFERRED_HEIGHT, Image.SCALE_DEFAULT));
				}

				setIcon(icon);
			}
		}

	}

	/*TheFileSystemViewclass localizes access to platform-specific file system information. Where 
	the JDK 1.1 version of java.io.Filewas fairly crippled in this respect, FileSystemViewfills in to 
	make it easier to design FileChooserUIobjects. The Swing FileSystemViewclass provides three 
	custom views as package-private subclasses of FileSystemView. They include support for UNIX 
	and Windows, plus a generic handler.
	Although it isn’t necessary to define your own FileSystemView, the class provides some 
	features that can be useful outside the context of a JFileChooser. To get the view specific to the 
	user’s runtime environment, call public static FileSystemView getFileSystemView(). The 
	class definition follows.*/

	/*public abstract class FileSystemView {
		// Constructors
		public FileSystemView(); // Properties
		// Properties
		public File getDefaultDirectory();
		public File getHomeDirectory();
		public File[] getRoots();
		// Class Methods
		public static FileSystemView getFileSystemView();
		// Other Methods
		public File createFileObject(File directory, String filename);
		public File createFileObject(String path);
		protected File createFileSystemRoot(File file);
		public abstract File createNewFolder(File containingDir) throws IOException;
		public File getChild(File parent, String filename);
		public File[] getFiles(File directory, boolean useFileHiding);
		public File getParentDirectory(File file);
		public String getSystemDisplayName(File file);
		public Icon getSystemIcon(File file);
		public String getSystemTypeDescription(File file);
		public boolean isComputerNode(File file);
		public boolean isDrive(File file);
		public boolean isFileSystem(File file);
		public boolean isFileSystemRoot(File file);
		public boolean isFloppyDrive(File file);
		public boolean isHiddenFile(File file);
		public boolean isParent(File folder, File file);
		public boolean isRoot(File file);
		public Boolean isTraversable(File file);
		
		} */

}
